import {
  editContent,findStudyMoudleList,verifyCodeFun
  } from './talkCollegeServ'
  // import { parse } from 'qs'
  import { message } from 'antd'
  // import moment from 'moment'
  import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
  // import $ from 'jquery';
  
  const defultState = {
      biz:{
        queryForm: {}
      },
      moudleList: []
  }
  
  const tmpModule = {
    state: cloneDeep(defultState),
    subscriptions: {
      setup ({dispatch, history}) {
        history.listen(({ pathname, query }) => {
          if('' + tmpModule.pathname === '' + pathname){
            dispatch({type: 'updateStore', payload: cloneDeep(defultState)})
            dispatch({type: 'findStudyMoudleList', payload: {}})
          }
        })
      },
    },
  
  
    effects: {
    //获取资料类型
    *findStudyMoudleList({payload},{put,select,call}){
      let {moudleList} = yield select(state => state[tmpModule.namespace])
      let params = {}
      if(yield call(verifyCodeFun,{})){
      const { rows,success } = yield call(findStudyMoudleList, {...params})
       if(!rows.length){
         return
       }
        moudleList =  rows.map((item,index)=>{
          return {
            label: item.moudleName,
            value: item.moudleName,
          }
        })
        yield put({type: 'updateStore', payload: {moudleList}})
      }
      },
      *editContent({payload},{put,select,call}){
        let params = {}
        if(payload.type == "edit"){
          params = {
              id: payload.id,//#修改必填
              ifUse: payload.ifUse,//#选填 如果需要禁用的话 填N
          }
        }
        if(payload.type == "del"){
          params = {
            id: payload.id,//#修改必填
            ifDelete: "Y"//#选填 如果需要逻辑删除的话 填Y
        }
        }
        const { success, msg, data } = yield call(editContent, {...params})
        let defultState = yield select(state => state[tmpModule.namespace])
        if(success){
          if(payload.type == "edit"){
          message.success('切换成功')
          }
          if(payload.type == "del"){
            message.success('删除成功')
            }
          defultState.biz.refreshListDatas(true)
        }
      }
    },
    // 存入状态机
    reducers: {
      updateStore(preState, action) {
        return Object.assign({}, preState, action.payload)
      },
    }
  }
  
  export default tmpModule
  